package cn.stronglink.assetmanage.business.inventory.presenter;

import android.graphics.Bitmap;

import java.io.IOException;

import cn.stronglink.assetmanage.MainActivity;
import cn.stronglink.assetmanage.MyApplication;
import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.business.check.model.CheckDetailModel;
import cn.stronglink.assetmanage.business.inventory.model.InventoryDetailModel;
import cn.stronglink.assetmanage.business.inventory.view.detail.InventoryDetailView;
import cn.stronglink.assetmanage.dao.DaoManager;
import cn.stronglink.assetmanage.dao.DepartmentDao;
import cn.stronglink.assetmanage.entity.Department;
import cn.stronglink.assetmanage.entity.InventoryDetail;
import okhttp3.Call;
import okhttp3.Response;

import static cn.stronglink.assetmanage.R.drawable.user;

/**
 * Created by chencongcong on 2018/6/15.
 */

public class InventoryDetailPresenter implements InventoryDetailModel.onInventoryDetailListener{
    private InventoryDetailModel inventoryDetailModel;
    private InventoryDetailView inventoryDetailView;

    public InventoryDetailPresenter(InventoryDetailModel inventoryDetailModel, InventoryDetailView inventoryDetailView) {
        this.inventoryDetailModel = inventoryDetailModel;
        this.inventoryDetailView = inventoryDetailView;
        this.inventoryDetailModel.setInventoryDetailListener(this);
    }


    public InventoryDetailModel getInventoryDetailModel() {
        return inventoryDetailModel;
    }

    public void setInventoryDetailModel(InventoryDetailModel inventoryDetailModel) {
        this.inventoryDetailModel = inventoryDetailModel;
    }

    public InventoryDetailView getInventoryDetailView() {
        return inventoryDetailView;
    }

    public void setInventoryDetailView(InventoryDetailView inventoryDetailView) {
        this.inventoryDetailView = inventoryDetailView;
    }

    /**
     * 显示资产数据
     * @param rfid
     */
    public void showDetailData(String rfid) {
        InventoryDetail inventoryDetail = inventoryDetailModel.getInventoryDetailByRfid(rfid);
        if (inventoryDetail != null) {
            Bitmap bitmap = null;
            long roomIndex = inventoryDetail.getRoomName()!=null?inventoryDetailModel.getRoomIndex(inventoryDetail.getRoomName()):0;
            long brandIndex = inventoryDetail.getBrandName()!=null?inventoryDetailModel.getBrandIndex(inventoryDetail.getBrandName()):0;
            long typeIndex = inventoryDetail.getTypeName()!=null?inventoryDetailModel.getTypeIndex(inventoryDetail.getTypeName()):0;
            long useDepart = inventoryDetail.getUseBranchId_()!=null?inventoryDetailModel.getUseDepart(inventoryDetail.getUseBranchId_()):0;
            long dataCenterIndex = inventoryDetail.getDatacenterId_()!=null?inventoryDetailModel.getDataCenterIndex(inventoryDetail.getDatacenterId_()):0;
            long userDepart = inventoryDetail.getManageBranchId_()!=null?inventoryDetailModel.getUseDepart(inventoryDetail.getManageBranchId_()):0;
            inventoryDetailView.showAssetDetail(inventoryDetail,
                    roomIndex,
                    brandIndex,
                    typeIndex,
                    useDepart,
                    dataCenterIndex,
                    userDepart);
        }
    }

    /**
     *保存数据
     * @param inventoryDetail
     */
    public void saveDetailData(InventoryDetail inventoryDetail){
        InventoryDetail saveInventoryDetail=inventoryDetailView.saveAssetDetail(inventoryDetail);
        inventoryDetailModel.saveInventoryData(saveInventoryDetail);
    }

    /**
     * 同步资产数据
     * @param inventoryDetail
     */
    public void syncDetailData(InventoryDetail inventoryDetail){
        InventoryDetail syncInventoryDetail=inventoryDetailView.saveAssetDetail(inventoryDetail);
        inventoryDetailModel.syncInventoryData(syncInventoryDetail);
    }

    @Override
    public void onFailure(Call call, IOException e) {
        inventoryDetailView.hideProgressDialog();
        inventoryDetailView.showToast(e.getMessage());
    }

    @Override
    public void onResponse(Call call, Response response) {
        try {
            inventoryDetailView.hideProgressDialog();
            if (response.isSuccessful()) {
                if (response.code() == 200) {
                    //删除已经同步的数据
                    inventoryDetailModel.deleteSyncData();
                } else {
                    inventoryDetailView.showToast(response.message());
                }
            } else {
                inventoryDetailView.showToast(MyApplication.APP_CONTEXT.getString(R.string.get_data_fail));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
